package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.AccountMapper;
import com.zretc.entity.Account;
import com.zretc.service.AccountService;
import com.zretc.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 何亮龙
 * 用户操作服务层实现类
 */
@Service
public class AccountServiceImpl implements AccountService {
    private static final Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public int register(Account account) {
        String accountPwd = account.getAccountPwd();
        accountPwd = MD5Util.getMd5(accountPwd);
        Integer accountId = account.getAccountId();
        String accountName = account.getAccountName();
        String accountGender = account.getAccountGender();
        String accountPicture = account.getAccountPicture();
        String accountPhone = account.getAccountPhone();
        int insert = accountMapper.insertAccount(new Account(accountId, accountName, accountGender, accountPwd, accountPhone, accountPicture,null));
        return insert;
    }

    @Override
    public boolean existsOfAccountName(String accountName) {
        Account findByAccountName = accountMapper.findByAccountsName(accountName);
        return findByAccountName != null ? true : false;
    }

    @Override
    public Account getByAccountName(String accountName) {
        Account account = accountMapper.findByAccountsName(accountName);
        return account;
    }

    @Override
    public Account getByAccountId(Integer accountId) {
        return accountMapper.findByAccountsId(accountId);
    }

    @Override
    public Account login(String accountName, String accountPwd) {
        accountPwd = MD5Util.getMd5(accountPwd);
        return accountMapper.findByAccountNameAndPwd(accountName, accountPwd);
    }

    @Override
    public boolean deleteByAccountId(Integer accountId) {
        int n = accountMapper.deleteAccountByAccountId(accountId);
        if (n > 0) {
            logger.info("====================================单条用户信息删除成功" );
            return true;
        } else {
            logger.info("========================单条用户信息失败===========================================" );
            return false;
        }
    }

    @Override
    public PageInfo<Account> getAccountByCondition(Map<String, String> condition) {
        // 获取页码
        String num = condition.get("pageNum");
        if (num == null) {
            num = "1";
        }
        Integer pageNum = Integer.valueOf(num);
        // 获取显示数量
        String size = condition.get("pageSize");
        if (size == null) {
            size = "10";
        }
        //用户ID
        String accountId = condition.get("accountId");
        //用户名称
        String accountName = condition.get("accountName");
        // 用户手机号
        String accountPhone = condition.get("accountPhone");
        // 用户性别
        String accountGender = condition.get("accountGender");

        Map<String, String> map = new HashMap<>();
        if ("".equals(accountId) || accountId == null) {
            map.put("accountId",null);
        } else {
            map.put("accountId",accountId);
        }
        if ("".equals(accountName) || accountName == null) {
            map.put("accountName",null);
        } else {
            map.put("accountName","%" + accountName + "%");
        }
        if ("".equals(accountPhone) || accountPhone == null) {
            map.put("accountPhone",null);
        } else {
            map.put("accountPhone","%" + accountPhone + "%");
        }
        if ("".equals(accountGender) || accountGender == null) {
            map.put("accountGender",null);
        } else {
            map.put("accountGender",accountGender);
        }
        Integer pageSize = Integer.valueOf(size);
        PageHelper.startPage(pageNum, pageSize);
        List<Account> accountsList = accountMapper.findAccountsList(map);
        PageInfo<Account> pageInfo = new PageInfo<Account>(accountsList);
        return pageInfo;
    }


}
